package com.deliinsns.sys.util;
import javax.servlet.http.HttpServletRequest;
public class RequestUtils {
	/**
	 * 是否为POST请求
	 *
	 * @param request 请求对象
	 * @return 是否为post请求
	 */
	public static boolean isPost(HttpServletRequest request) {
		String method = request.getMethod();
		return "POST".equalsIgnoreCase(method);
	}

	/**
	 * 是否为Ajax请求
	 *
	 * @param request 请求对象
	 * @return 是否为ajax请求
	 */
	public static boolean isAjax(HttpServletRequest request) {
		String header = request.getHeader("X-Requested-With");
		return header != null && "XMLHttpRequest".equalsIgnoreCase(header);
	}

	/**
	 * 获取客户端的ip地址
	 *
	 * @param request 请求对象
	 * @return ip地址
	 */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
}
